package com.xu.controller;

import com.xu.pojo.*;
import com.xu.pojo.vo.*;
import com.xu.service.ItemService;
import com.xu.utils.JSONResult;
import com.xu.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author anan
 * @create 2022-01-11 15:27
 */

@RestController
@RequestMapping("items")
@Api(value = "商品详情", tags = "查询商品信息")
public class ItemsController extends BaseController{
    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public JSONResult sixNewItems(
            @ApiParam(name = "itemId", value = "商品ID", required = true)
            @PathVariable String itemId){

        if(itemId == null) return JSONResult.errorMsg("商品不存在");
        Items item = itemService.querryItemById(itemId);
        List<ItemsImg> itemImgList = itemService.querryItemImgList(itemId);
        List<ItemsSpec> itemSpecList = itemService.querryItemSpecList(itemId);
        ItemsParam itemParams = itemService.querryItemParam(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(item);
        itemInfoVO.setItemImgList(itemImgList);
        itemInfoVO.setItemSpecList(itemSpecList);
        itemInfoVO.setItemParams(itemParams);

        //System.out.println(list);
        return JSONResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "查询商品评价等级", notes = "查询商品评价等级", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public JSONResult commentLevel(
            @ApiParam(name = "itemId", value = "商品ID", required = true)
            @RequestParam String itemId){

        if(StringUtils.isBlank(itemId)) return JSONResult.errorMsg(null);
        CommentLevelCountsVO commentLevelCountsVO = itemService.queryCommentCounts(itemId);
        return JSONResult.ok(commentLevelCountsVO);
    }

    @ApiOperation(value = "查看商品评论", notes = "查看商品评论", httpMethod = "GET")
    @GetMapping("/comments")
    public JSONResult commentLevel(
            @ApiParam(name = "itemId", value = "商品ID", required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level", value = "评价等级", required = false)
            @RequestParam Integer level,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "查询分页每一页的显示条数", required = false)
            @RequestParam Integer pageSize) {

        if(StringUtils.isBlank(itemId)) return JSONResult.errorMsg(null);
        if(page == null){
            page = 1;
        }
        if(pageSize == null){
            pageSize = COMMEN_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = itemService.queryPagedComments(itemId, level, page, pageSize);
        return JSONResult.ok(pagedGridResult);
    }

    @ApiOperation(value = "搜索商品列表", notes = "搜索商品列表", httpMethod = "GET")
    @GetMapping("/search")
    public JSONResult searchItems(
            @ApiParam(name = "keywords", value = "关键字", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "查询分页每一页的显示条数", required = false)
            @RequestParam Integer pageSize) {

        if(StringUtils.isBlank(keywords)) return JSONResult.errorMsg(null);
        if(page == null){
            page = 1;
        }
        if(pageSize == null){
            pageSize = PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = itemService.searchItems(keywords, sort, page, pageSize);
        return JSONResult.ok(pagedGridResult);
    }

    @ApiOperation(value = "通过分类id搜索商品列表", notes = "通过分类id搜索商品列表", httpMethod = "GET")
    @GetMapping("/catItems")
    public JSONResult catItems(
            @ApiParam(name = "catId", value = "三级分类id", required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "查询分页每一页的显示条数", required = false)
            @RequestParam Integer pageSize) {

        if(catId == null) return JSONResult.errorMsg(null);
        if(page == null){
            page = 1;
        }
        if(pageSize == null){
            pageSize = PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = itemService.searchItems(catId, sort, page, pageSize);
        return JSONResult.ok(pagedGridResult);
    }

    @ApiOperation(value = "根据商品规格ids查找最新的商品数据", notes = "根据商品规格ids查找最新的商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public JSONResult refresh(
            @ApiParam(name = "itemSpecIds", value = "拼接的ids", required = true, example = "1, 3, 5")
            @RequestParam String itemSpecIds
            ) {

        if(StringUtils.isBlank(itemSpecIds)){
            return JSONResult.ok();
        }
        List<ShopcartVO> shopcartVOS = itemService.queryItemsBySpecId(itemSpecIds);
        return JSONResult.ok(shopcartVOS);
    }

}
